app.controller('examinationManage', ['$scope','$document','$modal','$http','$rootScope','$state', function($scope,$document,$modal,$http,$rootScope,$state){
	// 打开交卷时间
	$scope.openAssignmentTime=function(e){
		e.stopPropagation();
		$(".dateSelect").show();
	}

	// 关闭交卷时间
	$scope.closeAssignmentTime=function(e){
		e.stopPropagation();
		$(".dateSelect").hide();

		$scope.getList();
	}

	$document.bind("click", function() {
		$(".dateSelect").hide();
	});

	// 查看详情
	$scope.viewDetails=function(item){
		var modalInstance = $modal.open({
		    templateUrl : 'viewDetails.html',  //指向创建的视图
		    controller : 'viewDetails',// 初始化模态范围
		    size : "lg",
		    backdrop:"static"
		});

		modalInstance.result.then(function() {

		}, function() {

		});
		$rootScope.userExamId=item.user_exam_id;
	}

	$scope.searchKey=[
		{
			"name":"用户账号",
			"id":"account"
		},
		{
			"name":"试卷名称",
			"id":"pname"
		}
	];
	$scope.nowSearchKey=$scope.searchKey[0];
	$scope.selectSearchKey=function(item){
		$scope.nowSearchKey=item;
		$scope.getList();
	}

	// 获取日期
	$scope.getDate=function(){
		var nowDate=new Date();
		var year=nowDate.getFullYear();
		var month=nowDate.getMonth()+1;
		if(month<10){
			month="0"+month;
		}
		var day=nowDate.getDate();
		if(day<10){
			day="0"+day;
		}

		var beforeDate = new Date(nowDate.getTime() - 7 * 24 * 3600 * 1000);
		var beforeYear=beforeDate.getFullYear();
		var beforeMonth=beforeDate.getMonth()+1;
		if(beforeMonth<10){
			beforeMonth="0"+beforeMonth;
		}
		var beforeDay=beforeDate.getDate();
		if(beforeDay<10){
			beforeDay="0"+beforeDay;
		}
		
		var startTimeArr=[beforeYear,beforeMonth,beforeDay];
		var endTimeArr=[year,month,day];
		$scope.startTime=startTimeArr.join("-");
		$scope.endTime=endTimeArr.join("-");
	}
	$scope.getDate();

	$scope.getList=function(){
		var data={
	    	page:1,
	    	page_size:15,
	    	start_time:$scope.startTime,
	    	end_time:$scope.endTime,
	    	search_key:$scope.nowSearchKey.id
	    }
	    if($scope.searchVal!=undefined){
	    	if($scope.searchVal.length!=0){
	    		data.search_val=$scope.searchVal;
	    	}
	    }
		$http({
		    url:$rootScope.baseUrl+"ExamRecord/getList",
		    method:"get",
		    params:data
		}).then(function(res){
		    if(res.data.code==200){
		    	$scope.examList=res.data.data.list;

		    	
		    	var total=res.data.data.total;

		    	if(res.data.data.length==0 || total==0){
		    		$scope.noData=true;
		    	}else {
		    		$scope.noData=false;
		    	}
		    	
		    	// 显示分页
		    	createPage(15,8,total);//每页15条数据，显示8个按钮
		    	function createPage(pageSize, buttons, total) {
		    	    $(".pagination").jBootstrapPage({
		    	        pageSize : pageSize,
		    	        total : total,
		    	        maxPageButton:buttons,
		    	        onPageClicked: function(obj, pageIndex) {
		    	            data.page=pageIndex+1;
		    	            $http({
		    	                url:$rootScope.baseUrl+'ExamRecord/getList',
		    	                method:"get",
		    	                params:data
		    	            }).then(function(res){
		    	                if(res.data.code==200){
		    	                	$scope.examList=res.data.data.list;

		    	                	if($scope.examList.length==0 && Math.ceil(total/15)==1){
		    	                		$scope.noData=true;
		    	                	}else {
		    	                		$scope.noData=false;
		    	                	}
		    	                }
		    	            },function(error){
		    	                console.log(error);
		    	            });
		    	        }
		    	    });
		    	}
		    }else if(res.data.code==409){
		    	$state.go("access.signin");
		    }
		},function(error){
		    console.log(error);
		});
	}
	$scope.getList();
}]);

// 查看详情
app.controller('viewDetails',['$scope','$modalInstance','$http','$rootScope','$sce','$state', function($scope,$modalInstance,$http,$rootScope,$sce,$state){
	$scope.userExamId=$rootScope.userExamId;
	$scope.cancel=function(){
		$modalInstance.close();
	}

	$http({
	    url:$rootScope.baseUrl+"ExamRecord/getDetail",
	    method:"post",
	    data:{
	    	user_exam_id:$scope.userExamId
	    }
	}).then(function(res){
	    if(res.data.code==200){
	    	$scope.detail=res.data.data;

	    	$scope.rightNum=0;
	    	$scope.errorNum=0;
	    	$.each($scope.detail,function(index,item){
	    		item.title=$sce.trustAsHtml(item.title);

	    		console.log(item.opt);

	    		$.each(item.opt,function(optIndex,option){
	    			option.option_val=$sce.trustAsHtml(option.option_val);
	    		});

	    		if(item.is_true==1){
	    			$scope.rightNum++;
	    		}else {
	    			$scope.errorNum++;
	    		}
	    	});
	    }else if(res.data.code==409){
	    	$state.go("access.signin");
	    }
	},function(error){
	    console.log(error);
	});
}]);